package cn.lger.icollege.controller;

import cn.lger.icollege.entity.Attendance;
import cn.lger.icollege.entity.Notice;
import cn.lger.icollege.entity.Student;
import cn.lger.icollege.service.AttendanceService;
import cn.lger.icollege.service.NoticeService;
import cn.lger.icollege.service.StudentService;
import cn.lger.icollege.util.FileUploadUtil;
import cn.lger.icollege.util.PageableGenerator;
import cn.lger.icollege.util.SecurityResourceUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;

/**
 * Code that Changed the World
 * Pro said
 * Created by Pro on 2018-04-16.
 */
@RestController
@RequestMapping("/stu")
public class StudentController {

    @Value("${file.upload.path}")
    private String prefixPath;

    @Resource
    private StudentService studentService;
    @Resource
    private NoticeService noticeService;
    @Resource
    private AttendanceService attendanceService;

    @GetMapping("/student")
    public Student getStudent() {
        String username = SecurityResourceUtil.getUserDetailsUsername();
        return studentService.findStudentByUsername(username);
    }

    @PostMapping("/student")
    public Student updateStudent( Student student, MultipartFile file) {
        String username = SecurityResourceUtil.getUserDetailsUsername();
        student.setIconPath(FileUploadUtil.uploadFileToFolder(file, prefixPath, "icon"));
        student.setUsername(username);
        return studentService.updateStudent(student);
    }

    @GetMapping("/notices")
    public Page<Notice> getNotices(Long classId, Integer currentPage, Integer pageSize){
        if(classId!=null){
            return noticeService.findNoticesByClassId(classId, PageableGenerator.pageOf(currentPage, pageSize));
        }
        return null;
    }

    @GetMapping("/attendances")
    public Page<Attendance> getAttendances(Integer currentPage, Integer pageSize,String teacherUsername){
        String username = SecurityResourceUtil.getUserDetailsUsername();
        if(teacherUsername==null||"".equals(teacherUsername.trim())){
            return attendanceService.findAllByStudentUsername(PageableGenerator.pageOf(currentPage, pageSize),username);
        }else {
            return attendanceService.findAllByStudentUsernameAndTeacherUsername(PageableGenerator.pageOf(currentPage, pageSize),username,teacherUsername);
        }
    }

    @PostMapping("/message")
    public boolean putAttendance(@NotNull String attendanceId, String msg, MultipartFile file){
        String username = SecurityResourceUtil.getUserDetailsUsername();
        String filePath = " ";
        try {
            if (file != null && !file.isEmpty()){
                filePath = FileUploadUtil.upload(file, prefixPath,"download", FileUploadUtil.randomUUID());
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("文件上传失败");
        }
        return studentService.sendMessageToTeacher(username, attendanceId,msg, filePath);
    }


}
